package com.whc.gulimall.scekill.controller;

import com.whc.common.utils.R;
import com.whc.gulimall.scekill.service.SecKillService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author angaoen
 */
@Controller
@RequestMapping("/seckill")
public class SecKillController {

    @Resource
    private SecKillService secKillService;

    /**
     * 获取最近秒杀的商品信息
     *
     * @return 最近秒杀的商品信息
     */
    @GetMapping
    @ResponseBody
    public R listSecKillInfo() {
        return R.ok().setData(secKillService.listSecKillInfo());
    }

    /**
     * 通过skuId获取到sku在redis秒杀中的信息
     *
     * @param skuId skuId
     * @return sku在redis秒杀中的信息
     */
    @GetMapping("/{skuId}")
    @ResponseBody
    public R getSkuInfoBySkuId(@PathVariable("skuId") Long skuId) {
        return R.ok().setData(secKillService.getSkuInfoBySkuId(skuId));
    }

    /**
     * 秒杀商品
     *
     * @param killId     skuId_promotionSessionId
     * @param randomCode 随机码
     * @param num        抢购书
     * @return 订单号
     */
    @GetMapping("/kill")
    public String killProduct(@RequestParam("killId") String killId,
                              @RequestParam("key") String randomCode,
                              @RequestParam("num") Integer num,
                              Model model) {
        model.addAttribute("orderSn", secKillService.kill(killId, randomCode, num));
        return "killed";
    }

}
